/* Utilities */

function findElement(id) {
	if (document.getElementById) return document.getElementById(id);
	else if (document.layers) return document.layers[id];
	else if (document.all) return document.all[id];
	else return false;
}

function setCookie(cName, cValue, cDays) {
	cExpires = ""
	if (cDays) {
		var exDate = new Date()
		exDate.setTime(exDate.getTime() + (cDays * 24 * 60 * 60 * 1000));
		var cExpires = "; expires=" + exDate.toGMTString();
	}
	document.cookie = cName + "=" + cValue + cExpires + "; path=/";
}

function getCookie(cName) {
	if (document.cookie.length > 0) {
		cStart = document.cookie.indexOf(cName + "=")
		if (cStart != -1) { 
			cStart = cStart + cName.length+1
			cEnd = document.cookie.indexOf("; expires", cStart)
			if (cEnd == -1) cEnd = document.cookie.length
    		return unescape(document.cookie.substring(cStart, cEnd))
		} 
	}
	return null
}

function delCookie(cName) {
  var exDate = new Date ( );
  exDate.setTime(exDate.getTime()-1);
  document.cookie = cName + "=; expires=" + exDate.toGMTString();
}

function detectWindowSize() {
	var 
		myWidth = 0; 
		myHeight = 0;

	if (typeof(window.innerWidth) == 'number') { // Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { // IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) { // IE 4
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return new Array(myWidth, myHeight);
}

/* Switch between styles */

var
	supportStyles = (document.styleSheets) ? 1 : 0;
	isie = ((ind1 = navigator.appVersion.indexOf("MSIE")) > -1)
? 1 : 0;
	isie4 = (isie && document.all) ? 1:0;
	isnn = (navigator.appName.indexOf("Netscape")>-1) ? 1 : 0;
	isnn4 = (isnn && document.layers) ? 1 : 0;
	isnn6 = (isnn && document.getElementById) ? 1:0;
	rules = function (id) {
		if (isie4) return document.styleSheets[id].rules;
		if (isnn6) return document.styleSheets[id].cssRules;
	}

function showStrict() {
	document.getElementsByTagName("link")[5].disabled = false;
	findElement("strictlayout").style.display = "none";
	findElement("widelayout").style.display = "block";
}

function showWide() {
	document.getElementsByTagName("link")[5].disabled = true;
	findElement("widelayout").style.display = "none";
	findElement("strictlayout").style.display = "block";
}

var 
	numStyle = (getCookie("strict")) ? 1 : 0;
	try { numSize = rules(numStyle)[8].style.fontSize.replace("em","") }
	catch(e) { numSize = "1.0" }
	newSize = parseFloat(numSize, 10);

function incrFonts() {
	newSize += 0.1;
	document.getElementsByTagName("body")[0].style.fontSize = newSize+"em";
}

function dimFonts() {
	newSize -= 0.1;
	document.getElementsByTagName("body")[0].style.fontSize = newSize+"em";
}
 
/* Execute on window load */

window.onload = function() {
	if (supportStyles) {
		findElement("layoutmode").style.display = "block"
		showWide();
		if (detectWindowSize()[0] <= 800 || getCookie("strict") == 1) showStrict()
		findElement("strictlayout").getElementsByTagName("a")[0].href = 'javascript:showStrict(); setCookie("strict", 1, 365);';
		findElement("widelayout").getElementsByTagName("a")[0].href = 'javascript:showWide(); setCookie("strict", 0, 0);';
		findElement("incrfonts").getElementsByTagName("a")[0].href = "javascript:incrFonts()";
		findElement("dimfonts").getElementsByTagName("a")[0].href = "javascript:dimFonts()";
	}
}
